package sf.database.jdbc.sql;

import sf.core.DBField;
import sf.database.jdbc.type.TypeHandler;
import sf.database.meta.ColumnMapping;
import sf.database.meta.TableMapping;

/**
 * sql 参数描述，包含值，对应的名称，如
 * <pre>
 * where id=${p(id)}
 * </pre>
 * 值是id对应的某个java对象，名字就是”id“
 */
public class SQLParameter {
    /**
     * 值
     */
    private Object value;
    /**
     * 表达式(可以代表参数名称,在命名查询中有用)
     */
    private String expression;
    //默认为0，不做处理，否则，会将目标对象转成期望的方式插入到数据库，比如long转short ？？
    public int jdbcType = 0;
    private TypeHandler<?> handler;
    /**
     * java类型 r2dbc中有用
     */
    private Class<?> javaType;
    /**
     * 字段(可以代表参数名称,在命名查询中有用)
     */
    private DBField field;
    private ColumnMapping columnMapping;
    private TableMapping tableMapping;

    public SQLParameter() {

    }

    public SQLParameter(Object value) {
        this.value = value;
    }

    public SQLParameter(Object value, ColumnMapping columnMapping) {
        this.value = value;
        this.columnMapping = columnMapping;
        if (columnMapping != null) {
            this.field = columnMapping.getField();
        }
    }

    public ColumnMapping getColumnMapping() {
        return columnMapping;
    }

    public SQLParameter setColumnMapping(ColumnMapping columnMapping) {
        this.field = columnMapping.getField();
        this.columnMapping = columnMapping;
        return this;
    }

    public String getExpression() {
        return expression;
    }

    public SQLParameter setExpression(String expression) {
        this.expression = expression;
        return this;
    }

    public Class<?> getJavaType() {
        return javaType;
    }

    public SQLParameter setJavaType(Class<?> javaType) {
        this.javaType = javaType;
        return this;
    }

    public DBField getField() {
        return field;
    }

    public TypeHandler<?> getHandler() {
        return handler;
    }

    public SQLParameter setHandler(TypeHandler<?> handler) {
        this.handler = handler;
        return this;
    }

    public int getJdbcType() {
        return jdbcType;
    }

    public SQLParameter setJdbcType(int jdbcType) {
        this.jdbcType = jdbcType;
        return this;
    }

    public TableMapping getTableMapping() {
        return tableMapping;
    }

    public void setTableMapping(TableMapping tableMapping) {
        this.tableMapping = tableMapping;
    }

    public Object getValue() {
        return value;
    }

    public SQLParameter setValue(Object value) {
        this.value = value;
        return this;
    }

    @Override
    public String toString() {
        if (value != null) {
            return value.toString();
        } else {
            return "";
        }
    }
}
